﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace cludg.Infrastructure.Mailer
{
    public class MailerToFolder : IMailer
    {
        protected readonly string FolderPath;

        public MailerToFolder(string folderPath)
            : base()
        {
            FolderPath = folderPath;
        }

        public void Send(string smtpAddr, string from, IEnumerable<string> to, IEnumerable<string> cc, string subj, string body)
        {
            Directory.CreateDirectory(FolderPath);
            string fileName = DateTime.Now.ToBinary().ToString() + ".txt";
            using (var writer = File.CreateText(Path.Combine(FolderPath, fileName)))
            {
                writer.WriteLine("from {0}", from);
                if (to != null)
                {
                    writer.Write("to");
                    foreach (var addr in to)
                        writer.Write(" {0}", addr);
                    writer.WriteLine();
                }
                if (cc != null)
                {
                    writer.Write("cc");
                    foreach (var addr in cc)
                        writer.Write(" {0}", addr);
                    writer.WriteLine();
                }
                writer.WriteLine(subj);
                writer.WriteLine(body);
            }
        }
    }
}
